// pages/edit/address-edit-page.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPhoneX: false,
    updateItem: {},
    name: '',
    mobile: '',
    address: '',
    is_default: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var title = "添加地址";
    if (typeof (options.dataItem) != "undefined") {
      title = "编辑地址";
      let item = JSON.parse(options.dataItem);
      this.setData({
        updateItem: item,
        name: item.name,
        mobile: item.mobile,
        address: item.address,
        is_default: item.is_default
      });
    }

    this.setData({
      isPhoneX: app.globalData.isIphoneX
    })

    wx.setNavigationBarTitle({
      title: title,
    })
  },
  textDidEndEdit: function (event) {
    var text_id = event.target.id;
    var textValue = event.detail.value;
    if (text_id === "name") {
      this.setData({
        name: textValue
      })
    }
    else if (text_id === "mobile") {
      this.setData({
        mobile: textValue
      })
    }
    else if (text_id === "address") {
      this.setData({
        address: textValue
      })
    }
  },
  action_check: function (event) {
    let check = event.detail.checked;
    this.setData({
      is_default: check
    })
  },
  //  添加地址
  action_save: function () {
    if (this.data.updateItem.id == null) {
      //  新增地址
    }
    else {
      //  修改地址
    }
    console.log('name:', this.data.name, '\naddress:', this.data.address, '\nmobile:', this.data.mobile, "是否为默认地址:", this.data.is_default);
  }
})